{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6dc6b825",
   "metadata": {},
   "source": [
    "------\n",
    "* Python Web crawler\n",
    "* 2023.11.22\n",
    "* week12_可视化web输出\n",
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64a020f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
      "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [22/Nov/2023 20:16:01] \"GET / HTTP/1.1\" 200 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask #python 后端与前端连接的一个框架\n",
    "from jinja2 import Markup, Environment, FileSystemLoader # jinja2 前端页面展示的框架\n",
    "from pyecharts.globals import CurrentConfig # pyecharts: A Python Echarts Plotting Libiary\n",
    "\n",
    "CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader(\"./templates\"))\n",
    "# Pyecharts模版 需要在 github 下载 templates 的模版文件\n",
    "\n",
    "from pyecharts import options as opts \n",
    "# options图表 的配置项 graph\n",
    "\n",
    "from pyecharts.charts import Bar\n",
    "# Bar柱状图 pyecharts 封装好的图形，可参见 gallery示例\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "# 1.找到合适的可视化图表，找到gallery示例代码，测试一下示例代码\n",
    "# 2.准备数据分析结果的参数，替换示例代码的参数\n",
    "# 3.创建制作图的函数\n",
    "def bar_base() -> Bar: \n",
    "    c = ( Bar()\n",
    "         .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]) \n",
    "         .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90]) \n",
    "         .add_yaxis(\"商家B\", [15, 25, 16, 55, 48, 8]) \n",
    "         .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    ) \n",
    "    return c \n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "    Geo()\n",
    "        .add_schema(napype=\"china\")\n",
    "        .add(\n",
    "        \"\",\n",
    "        [(\"广州\", 55), (\"北京\", 66), (\"杭州\", 77), (\"重庆\", 88)],\n",
    "        type_=ChartType.EFFECT_SCATTER,\n",
    "        color=\"white\",\n",
    "    )\n",
    "    .add(\n",
    "        \"geo\",\n",
    "        [(\"广州\", \"上海\"), (\"广州\", \"北京\"), (\"广州\", \"杭州\"), (\"广州\", \"重庆\")],\n",
    "        type_=ChartType.LINES,\n",
    "        effect_opts=opts.EffectOpts(\n",
    "            symbol=SymbolType.ARROW, symbol_size=6, color=\"blue\"\n",
    "        ),\n",
    "        linestyle_opts=opts.LineStyleOpts(curve=0.2),\n",
    "    )\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"Geo-Lines-background\"))\n",
    "    )\n",
    "    return c    \n",
    "\n",
    "# 4.将可视化图形通过markup方法，绘制在HTML的页面上\n",
    "@app.route(\"/\")\n",
    "def index(): \n",
    "    c = bar_base() \n",
    "    return Markup(c.render_embed()) \n",
    "\n",
    "if __name__ == \"__main__\": \n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8e87ac8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask #python 后端与前端连接的一个框架\n",
    "from jinja2 import Markup, Environment, FileSystemLoader #jinjia2前端页面展示的框架\n",
    "from pyecharts.globals import CurrentConfig #pyecharts：A python Echarts Plotting Libray\n",
    "\n",
    "CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader(\"./templates\"))\n",
    "# pyecharts 模板 需要在GitHub下载tekplates的模板文件\n",
    "\n",
    "from pyecharts import options as opts\n",
    "# options 图表的配置项 graph \n",
    "\n",
    "from pyecharts.charts import Bar\n",
    "# Bar 柱状图 pyecharts 封装好的图形 可参见gallery示例\n",
    "from pyrcharts.charts import Geo\n",
    "from pyecharts.globals import ChartType, SymbolType\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "# 找到合适的可视化图表： 找到gallery示例代码，测试以下示例代码，然后准备数据分析结果的参数，替换示例代码的参数。\n",
    "# 创建制作图的函数\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "        .add_yaxis(\"商家B\", [15, 25, 16, 55, 48, 8])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "def geo_lines():\n",
    "    c = (\n",
    "    Geo()\n",
    "        .add_schema(napype=\"china\")\n",
    "        .add(\n",
    "        \"\",\n",
    "        [(\"广州\", 55), (\"北京\", 66), (\"杭州\", 77), (\"重庆\", 88)],\n",
    "        type_=ChartType.EFFECT_SCATTER,\n",
    "        color=\"white\",\n",
    "    )\n",
    "    .add(\n",
    "        \"geo\",\n",
    "        [(\"广州\", \"上海\"), (\"广州\", \"北京\"), (\"广州\", \"杭州\"), (\"广州\", \"重庆\")],\n",
    "        type_=ChartType.LINES,\n",
    "        effect_opts=opts.EffectOpts(\n",
    "            symbol=SymbolType.ARROW, symbol_size=6, color=\"blue\"\n",
    "        ),\n",
    "        linestyle_opts=opts.LineStyleOpts(curve=0.2),\n",
    "    )\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"Geo-Lines-background\"))\n",
    " )\n",
    "return c   \n",
    "\n",
    "# 将可视化图形通过markup\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    c = bar_base()\n",
    "    return Markup(c.render_embed())\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb784c70",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
